/*
 * Utility methods
 * */
package com.example.gametestproject;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Utility {

	public static int dip2px(Context context, float dipValue)
	{
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager manager = (WindowManager)ResourceManager.getInstance().mContext.getSystemService(Context.WINDOW_SERVICE);
		manager.getDefaultDisplay().getMetrics(metrics);
		float scale = metrics.density;
		int dp = metrics.densityDpi;
		int xp = metrics.widthPixels;
		int yp = metrics.heightPixels;
		System.out.println("Param is: " + dp + " " + xp + " " + yp + " " + metrics.xdpi + " " + metrics.ydpi);
		return (int)(dipValue*scale+0.5f);
	}
	
	public static int px2dip(Context context, float pxValue)
	{
		float scale = context.getResources().getDisplayMetrics().density;
		return (int)(pxValue / scale + 0.5f);
	}
	
	/*convert pixel point to dip point*/
	public static Point point2Dip(Context context, Point pxPoint)
	{
		return new Point(Utility.px2dip(context, pxPoint.x), Utility.px2dip(context, pxPoint.y));
	}
	
	/*convert dip point to pixel point*/
	public static Point point2Pixel(Context context, Point dipPoint)
	{
		return new Point(Utility.dip2px(context, dipPoint.x), Utility.dip2px(context, dipPoint.y));
	}
	
	/*convert pixel rect to dip rect*/
	public static Rect rect2Dip(Context context, Rect pxRect)
	{
		int top = Utility.px2dip(context, pxRect.top);
		int left = Utility.px2dip(context, pxRect.left);
		int right = Utility.px2dip(context, pxRect.right);
		int bottom = Utility.px2dip(context, pxRect.bottom);
		return new Rect(left, top, right, bottom);
	}
	
	/*convert dip rect to pixel rect*/
	public static Rect rect2Pixel(Context context, Rect dipRect)
	{
		int top = Utility.dip2px(context, dipRect.top);
		int left = Utility.dip2px(context, dipRect.left);
		int right = Utility.dip2px(context, dipRect.right);
		int bottom = Utility.dip2px(context, dipRect.bottom);
		return new Rect(left, top, right, bottom);
	}

}
